//
//  MyViewController.m
//  DayDayRun
//
//  Created by 卫建 on 16/1/6.
//  Copyright © 2016年 卫建. All rights reserved.
//

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //图片圆角
    self.mineheadImageView.layer.cornerRadius = 35;
    self.mineheadImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.mineheadImageView.layer.borderWidth = 0.5;
    
    //设置代理
    self.mineTableView.dataSource = self;
    self.mineTableView.delegate = self;
    
    self.cellArray = @[@"历史记录",@"清除缓存",@"关于我们"];
    
    //cell如果是xib文件，使用下面的方法去注册
    //自定义MineTableViewCell
    [self.mineTableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineTableViewCell"];
    //自定义ExitTableViewCell
    [self.mineTableView registerNib:[UINib nibWithNibName:@"ExitTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ExitTableViewCell"];
    //自定义HealthTableViewCell
    [self.mineTableView registerNib:[UINib nibWithNibName:@"HealthTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HealthTableViewCell"];
    
     //更换头像
    [self changedHead];
    
}

#pragma mark - 更换头像
-(void)changedHead{
    
    //添加手势
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changedHeadAction:)];
    
    [self.mineheadImageView addGestureRecognizer:tapGestureRecognizer];
    
    //获取用户缓存信息头像
    AVQuery *query = [AVQuery queryWithClassName:@"_File"];
    [query whereKey:@"name" equalTo:[NSString stringWithFormat:@"%@.png",[AVUser currentUser].username]];
    [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        if (object != nil) {
            NSString *strUrl = [object objectForKey:@"url"];
            [self.mineheadImageView sd_setImageWithURL:[NSURL URLWithString:strUrl]];
        }
//        NSLog(@"+++++++++%@",error);
    }];
    
}

#pragma mark - 更换头像方法
-(void)changedHeadAction:(UITapGestureRecognizer *)sender{
    
    self.alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    //打开相机按钮
    UIAlertAction *openCamera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        //调用系统相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.delegate = self;
            picker.allowsEditing = YES;
            [self presentViewController:picker animated:YES completion:nil];
        }
        else
        {
            NSLog(@"没有相机");
        }
        
    }];
    
    //打开相册按钮
    UIAlertAction *openPhoto = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        //调用系统相册
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
        
    }];
    
    //取消按钮
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    //添加事件
    [self.alertController addAction:openCamera];
    [self.alertController addAction:openPhoto];
    [self.alertController addAction:cancel];
    
    //模态出控制器
    [self presentViewController:self.alertController animated:YES completion:nil];
    
}

#pragma mark - 协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        self.mineheadImageView.image = image;
    } else {
        self.mineheadImageView.image = image;
    }
    
    
    //存储用户缓存信息头像
    NSData *data = UIImagePNGRepresentation(self.mineheadImageView.image);
    AVFile *file = [AVFile fileWithName:[NSString stringWithFormat:@"%@.png",[AVUser currentUser].username] data:data];
    [file saveInBackground];
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
